
local module = _G["BlinkHealthTextModule"]

if select(2, UnitClass("player")) ~= "PALADIN" or not module then return end

local enabled_the_art_of_war = false
local L_GRAND_CRUSADER = "Grand Crusader"
local L_AVENGERS_SHIELD = "Avenger's Shield"
local L_THE_ART_OF_WAR = "The Art of War"
local L_TEMPLARS_VERDICT = "Templar's Verdict"
local L_SHIELD_OF_THE_RIGHTEOUS = "Shield of the Righteous"
local L_DIVINE_PURPOSE = "Divine Purpose"
local L_EXORCISM = "Exorcism"
if GetLocale()=="koKR" then
L_GRAND_CRUSADER = "위대한 성전사"
L_AVENGERS_SHIELD = "응징의 방패" -- id 31935
L_THE_ART_OF_WAR = "전쟁의 기술" -- id 87138
L_TEMPLARS_VERDICT = "기사단의 선고" -- id 85256
L_SHIELD_OF_THE_RIGHTEOUS = "정의의 방패" -- id 53600
L_DIVINE_PURPOSE = "신성한 목적" -- id 86172
L_EXORCISM = "퇴마술"
end

module.config = {
	[L_GRAND_CRUSADER] = {
		spellid = 85043
	},
	[L_THE_ART_OF_WAR] = {
		spellid = 87138,
		event = {"SPELL_ACTIVATION_OVERLAY_SHOW", "SPELL_ACTIVATION_OVERLAY_HIDE"}
	},
	[L_TEMPLARS_VERDICT] = {
		spellid = 85256
	},
	[L_SHIELD_OF_THE_RIGHTEOUS] = {
		spellid = 53600
	},
	[L_DIVINE_PURPOSE] = {
		spellid = 86172,
		talent = 15
	},
	[L_EXORCISM] = {
		spellid = 879
	},
}

local function getSize(baseSize, expirationTime, duration)
	local size = (expirationTime-GetTime()) / duration
	if size < 0.3 then
		size = baseSize * .8
	elseif size < 0.6 then
		size = baseSize * 1.0
	else
		size = baseSize * 1.2
	end
	return size / 2
end

function module:init()
	self.addon.mainFrame:RegisterEvent("PLAYER_ALIVE")
	self.addon.mainFrame:RegisterEvent("LEARNED_SPELL_IN_TAB")
	self.addon.mainFrame:RegisterEvent("CHARACTER_POINTS_CHANGED")
	--self.addon.mainFrame:RegisterEvent("UNIT_POWER")
	self:update()
end

function module:update()
	self.primary = GetSpecialization()
	for auraName, t in pairs(self.config) do
		if t.talent and t.talent > 0 then
			local name, iconTexture, tier, column, selected, available = GetTalentInfo(t.talent);
			if name and available then
				self.config[auraName].use = true
				if t.event and self.addon and self.addon.mainFrame then
					if type(t.event)=='table' then
						for i=1, #t.event do
							self.addon.mainFrame:RegisterEvent(t.event[i])
						end
					else
						self.addon.mainFrame:RegisterEvent(t.event)
					end
				end
			end
		else
			self.config[auraName].use = false
			if IsPlayerSpell(t.spellid) then
				self.config[auraName].use = true
				if t.event and self.addon and self.addon.mainFrame then
					if type(t.event)=='table' then
						for i=1, #t.event do
							self.addon.mainFrame:RegisterEvent(t.event[i])
						end
					else
						self.addon.mainFrame:RegisterEvent(t.event)
					end
				end
			end
		end
	end
end

function module:LEARNED_SPELL_IN_TAB(...)
	self:update()
end
module.CHARACTER_POINTS_CHANGED = module.LEARNED_SPELL_IN_TAB
function module:PLAYER_ALIVE(...)
	self.addon.mainFrame:UnregisterEvent("PLAYER_ALIVE")
	self:update()
end
function module:SPELL_ACTIVATION_OVERLAY_SHOW(...)
	local spellID = ...
	if spellID == 59578 then
		enabled_the_art_of_war = true
	end
end
function module:SPELL_ACTIVATION_OVERLAY_HIDE(...)
	local spellID = ...;
	if spellID == 59578 then
		enabled_the_art_of_war = false
	end
end
function module:getPlayerText()
	local numHolyPower = UnitPower( "player", SPELL_POWER_HOLY_POWER )
	if numHolyPower > 0 then
		return (":|cff%02x%02x%02x%d|r"):format(255, 255, 63, numHolyPower)
	end
	return ""
end

function module:getTargetText()
	local text = ""
	local size = self.addon.db.fontSizeHealth
	local name, rank, icon, count, debufType, duration, expirationTime
	local r, g, b = 1, 1, 1


	local already_display_holypower_spell = false
	if self.config[L_DIVINE_PURPOSE].use then
		name, rank, icon, count, debufType, duration, expirationTime = UnitAura("player", L_DIVINE_PURPOSE, nil, "HELPFUL")
		if name then
			if self.primary == 1 then
				_, _, icon = GetSpellInfo(L_DIVINE_PURPOSE)
			elseif self.primary == 2 then
				_, _, icon = GetSpellInfo(L_SHIELD_OF_THE_RIGHTEOUS)
			elseif self.primary == 3 then
				_, _, icon = GetSpellInfo(L_TEMPLARS_VERDICT)
			end
			size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
			text = text .. (":|T%s:%d|t"):format(icon, size)
			already_display_holypower_spell = true
		end
	end

	if already_display_holypower_spell == false then
		if self.primary == 1 then
		elseif self.primary == 2 then
			local numHolyPower = UnitPower( "player", SPELL_POWER_HOLY_POWER )
			if numHolyPower >= 3 then
				_, _, icon = GetSpellInfo(L_SHIELD_OF_THE_RIGHTEOUS)
				size = self.addon.db.fontSizeHealth / 2
				text = text .. (":|T%s:%d|t"):format(icon, size)
			end
		elseif self.primary == 3 then
			local numHolyPower = UnitPower( "player", SPELL_POWER_HOLY_POWER )
			if numHolyPower >= 3 then
				_, _, icon = GetSpellInfo(L_TEMPLARS_VERDICT)
				size = self.addon.db.fontSizeHealth / 2
				text = text .. (":|T%s:%d|t"):format(icon, size)
			end
		end
	end

	if self.config[L_GRAND_CRUSADER].use then
		name, rank, icon, count, debufType, duration, expirationTime = UnitAura("player", L_GRAND_CRUSADER, nil, "HELPFUL")
		if name then
			_, _, icon = GetSpellInfo(L_AVENGERS_SHIELD)
			size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
			text = text .. (":|T%s:%d|t"):format(icon, size)
		end
	end

	if self.config[L_THE_ART_OF_WAR].use and enabled_the_art_of_war==true then
		_, _, icon = GetSpellInfo(self.config[L_EXORCISM].spellid)
		if icon then
			size = self.addon.db.fontSizeHealth / 2
			text = text .. (":|T%s:%d|t"):format(icon, size)
		end
	end

	return text
end
